/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package View;

import Module.ActionList;
import Module.User;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

/**
 *
 * @author CCD gruppe 2
 */
public class ActionListFrame extends javax.swing.JFrame {

    /** Creates new form ActionListFrame */
    public ActionListFrame() {
        initComponents();
    }
    private javax.swing.JTextArea actionlistIntroTextArea;
    private javax.swing.JButton closeActivityListFrameButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable actionlistTable;
    private JComboBox statusBox;
    private JComboBox responsibleBox;
    private JTextField startDateTextField;
    private JTextField endDateTextField;
    private javax.swing.JButton regretButton;
    private javax.swing.JButton saveActionListButton;

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        actionlistIntroTextArea = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        actionlistTable = new javax.swing.JTable();
        closeActivityListFrameButton = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        saveActionListButton = new javax.swing.JButton();
        regretButton = new javax.swing.JButton();
        statusBox = new JComboBox();
        responsibleBox = new JComboBox();
        statusBox.removeAllItems();
        responsibleBox.removeAllItems();
        startDateTextField = new JTextField();
        endDateTextField = new JTextField();
        
        statusBox.addItem("Registrert");
        statusBox.addItem("Åpen");
        statusBox.addItem("Stengt");

        jLabel1.setText("Aksjonsliste");

        jLabel2.setText("Introduksjon");

        actionlistIntroTextArea.setColumns(20);
        actionlistIntroTextArea.setRows(5);
        actionlistIntroTextArea.setLineWrap(true);
        jScrollPane1.setViewportView(actionlistIntroTextArea);

        
        jScrollPane2.setViewportView(actionlistTable);

        closeActivityListFrameButton.setText("Lukk");

        jLabel3.setText("Aksjonsliste");
        saveActionListButton.setText("Lagre");

        regretButton.setText("Angre");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                            .add(251, 251, 251)
                            .add(jLabel1))
                        .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                            .addContainerGap()
                            .add(jLabel2))
                        .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                            .addContainerGap()
                            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 618, Short.MAX_VALUE))
                        .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                            .addContainerGap()
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                    .add(regretButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 186, Short.MAX_VALUE)
                                    .add(18, 18, 18)
                                    .add(saveActionListButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 200, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(18, 18, 18)
                                    .add(closeActivityListFrameButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 196, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                .add(jScrollPane2))))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jLabel3)))
                .addContainerGap(32, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel2)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 143, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(9, 9, 9)
                .add(jLabel3)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 343, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 20, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(closeActivityListFrameButton)
                    .add(saveActionListButton)
                    .add(regretButton))
                .addContainerGap())
        );

        pack();
        setCenterPos(this);
    }// </editor-fold>

    public static void setCenterPos(JFrame frame) {
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) ((screen.getWidth() - frame.getWidth()) / 2);
        int y = (int) ((screen.getHeight() - frame.getHeight()) / 2);
        frame.setLocation(x, y);
    }

    /*
     * Getters and setters
     */

    public JTextArea getActionlistIntroTextArea() {
        return actionlistIntroTextArea;
    }

    public void setActionlistIntroTextArea(JTextArea actionlistIntroTextArea) {
        this.actionlistIntroTextArea = actionlistIntroTextArea;
    }

    public JTable getActionlistTable() {
        return actionlistTable;
    }

    public void setActionlistTable(JTable actionlistTable) {
        this.actionlistTable = actionlistTable;
    }

    public JButton getCloseActivityListFrameButton() {
        return closeActivityListFrameButton;
    }

    public void setCloseActivityListFrameButton(JButton closeActivityListFrameButton) {
        this.closeActivityListFrameButton = closeActivityListFrameButton;
    }
    private void setUpUsers(JTable table, TableColumn status, ArrayList<User> users){
        responsibleBox.removeAllItems();
        for(int i=0; i<users.size();i++){
            responsibleBox.addItem(users.get(i).getFullname());
        }
        status.setCellEditor(new DefaultCellEditor(responsibleBox));
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        status.setCellRenderer(renderer);
    }

    public JButton getRegretButton() {
        return regretButton;
    }

    public JButton getSaveActionListButton() {
        return saveActionListButton;
    }

    public void setTable(ArrayList<ActionList> actionList, ArrayList<User> users){
        actionlistTable = new javax.swing.JTable(actionList.size(),5);

        setUpUsers(actionlistTable, actionlistTable.getColumnModel().getColumn(3),users);
        actionlistTable.getColumnModel().getColumn(4).setCellEditor(new DefaultCellEditor(statusBox));
        actionlistTable.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(startDateTextField));
        actionlistTable.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(endDateTextField));

        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        actionlistTable.getColumnModel().getColumn(4).setCellRenderer(renderer);
        actionlistTable.getColumnModel().getColumn(0).setHeaderValue("Beskrivelse");
        actionlistTable.getColumnModel().getColumn(1).setHeaderValue("Registreringsdato");
        actionlistTable.getColumnModel().getColumn(2).setHeaderValue("Ferdigstillelsesdato");
        actionlistTable.getColumnModel().getColumn(3).setHeaderValue("Ansvarlig");
        actionlistTable.getColumnModel().getColumn(4).setHeaderValue("Status");
        for(int i=0;i<actionList.size();i++){
            actionlistTable.getModel().setValueAt(actionList.get(i).getDescription(), i, 0);
            actionlistTable.getModel().setValueAt(actionList.get(i).getStartDate(), i, 1);
            actionlistTable.getModel().setValueAt(actionList.get(i).getEndDate(), i, 2);
            actionlistTable.getModel().setValueAt(actionList.get(i).getResponsible(), i, 3);
            actionlistTable.getModel().setValueAt(actionList.get(i).getStatus(), i, 4);
            actionlistTable.getColumnModel().getColumn(0).setCellRenderer(new TextAreaRenderer());
            if (actionList.get(i).getDescription().length() > 14) {
                int lines = (int) actionList.get(i).getDescription().length() / 14;
                lines+=1;
                actionlistTable.setRowHeight(i, actionlistTable.getRowHeight() * lines);
            }

        }
        jScrollPane2.setViewportView(actionlistTable);

    }

    public JTextField getEndDateTextField() {
        return endDateTextField;
    }

    public JTextField getStartDateTextField() {
        return startDateTextField;
    }

    private class TextAreaRenderer extends JTextArea
            implements TableCellRenderer {

        public TextAreaRenderer() {
            setLineWrap(true);
            setWrapStyleWord(true);
        }

        public Component getTableCellRendererComponent(JTable jTable,
                Object obj, boolean isSelected, boolean hasFocus, int row,
                int column) {
            setText((String) obj);
            return this;
        }
    }

}
